package com.atguigu.yygh.msm.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.atguigu.yygh.msm.config.ConstantPropertiesUtils;
import com.atguigu.yygh.msm.service.MsmService;
import com.atguigu.yygh.vo.msm.MsmVo;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ybl
 * @version 1.9
 * @date 2021/5/20 16:28
 */
@Service
public class MsmServiceImpl implements MsmService {


  // 订单通知信息
  @Override
  public boolean send(MsmVo msmVo) {
    if(!StringUtils.isEmpty(msmVo.getPhone())) {
      String code = (String)msmVo.getParam().get("code");
      return this.send(msmVo.getPhone(),code);
    }

    return false;
  }

  // 根据手机号发送验证码
  @Override
  public boolean send(String phone, String code) {
    // 判断手机号是否为空
    if (StringUtils.isEmpty(phone)){
      return false;
    }
    // 阿里云短信服务
    // 设置相关参数
    DefaultProfile profile = DefaultProfile
        .getProfile(ConstantPropertiesUtils.REGION_Id,ConstantPropertiesUtils.ACCESS_KEY_ID,ConstantPropertiesUtils.SECRECT);

    IAcsClient client = new DefaultAcsClient(profile);
    CommonRequest request = new CommonRequest();
    //request.setProtocol(ProtocolType.HTTPS);
    request.setMethod(MethodType.POST);
    request.setDomain("dysmsapi.aliyuncs.com");
    request.setVersion("2017-05-25");
    request.setAction("SendSms");
    //手机号
    request.putQueryParameter("PhoneNumbers", phone);
    //签名名称
    request.putQueryParameter("SignName", "智慧城市");
    //模板code
    request.putQueryParameter("TemplateCode", "SMS_205617460");
    //验证码  使用json格式   {"code":"123456"}
    Map<String,Object> param = new HashMap();
    param.put("code",code);
    request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));

    //调用方法进行短信发送
    try {
      CommonResponse response = client.getCommonResponse(request);
      System.out.println(response.getData());
      // 是否发送成功
      return response.getHttpResponse().isSuccess();
    } catch (ServerException e) {
      e.printStackTrace();
    } catch (ClientException e) {
      e.printStackTrace();
    }

    return false;
  }
}
